package leetcode;

import java.util.HashMap;

public class Q0290 {
    public boolean wordPattern(String pattern, String s) {
        String[] words = s.split(" ");
        if (words.length != pattern.length()) return false;
        HashMap<Character, String> mapCS = new HashMap<>();
        HashMap<String, Character> mapSC = new HashMap<>();
        for (int i = 0; i < pattern.length(); i++) {
            if (!mapCS.containsKey(pattern.charAt(i)) && !mapSC.containsKey(words[i])) {
                mapCS.put(pattern.charAt(i), words[i]);
                mapSC.put(words[i], pattern.charAt(i));
            } else if (mapCS.containsKey(pattern.charAt(i)) && mapSC.containsKey(words[i])) {
                if (!mapCS.get(pattern.charAt(i)).equals(words[i]) || mapSC.get(words[i]) != pattern.charAt(i))
                    return false;
            } else {
                return false;
            }
        }
        return true;
    }
}
